package application;

import java.io.Serializable;
import java.util.Collections;
import java.util.Vector;

public class HighScoreTable implements Serializable{
	private Vector<ScoreInformation> _data;
	private final int MAXSIZE = 10;
	/**
	 * constructor
	 */
	public HighScoreTable(){
		this._data = new Vector<ScoreInformation>(0);
	}
	/**
	 * adds score information to the list
	 * @param si the information to add
	 */
	public void addScore(ScoreInformation si){
		int newScore = si.get_score();
		int oldScore;
		if(this._data.size() >= MAXSIZE){
			oldScore = this._data.elementAt(MAXSIZE - 1).get_score();
			if(newScore > oldScore){
				this.removeScore();
				this._data.add(si);
			}
		}
		else
			this._data.add(si);
		this.Sort();
	}
	/**
	 * creates a ScoreInformation object from parameters and adds to list
	 * @param name player's name
	 * @param score player's score
	 */
	public void addScore(String name, String score) {
		ScoreInformation si = new ScoreInformation(name, Integer.valueOf(score));
		this.addScore(si);
	}
	/**
	 * remove a score from the list
	 * @param i
	 */
	public void removeScore(){
		this._data.remove(MAXSIZE-1);
	}
	/**
	 * sort the list
	 */
	public void Sort(){
		Collections.sort(this._data);
	}
	/**
	 * makes a two dimensional Object array, used for GUI table
	 * @return a two dimensional Object array, representing the high score table
	 */
	public Object[][] toObjectArrays(){
		Object[][] ans = new Object[MAXSIZE][3];
		for(int i = 0; i < this._data.size(); i++){
			ans[i][0] = new Integer(i+1);
			ans[i][1] = this._data.get(i).get_name();
			ans[i][2] = new Integer(this._data.get(i).get_score());
		}
		return ans;
	}
	
	public Vector<ScoreInformation> get_data() {
		return _data;
	}
	public void set_data(Vector<ScoreInformation> _data) {
		this._data = _data;
	}
	public int getMAXSIZE() {
		return MAXSIZE;
	}
}
